package com.magold.sound;

import java.io.IOException;

import org.andengine.audio.IAudioEntity;
import org.andengine.audio.sound.Sound;
import org.andengine.audio.sound.SoundFactory;
import org.andengine.entity.IEntity;

import android.content.Context;
import android.util.Log;

import com.magold.context.IContext;

/**
 * Specification:
 * 	new {@link DefaultNoiseMaker#DefaultNoiseMaker(float, IEntity, String)},
 *  get {@link SoundNotifier} by {@link IContext#getSoundNotifier()},
 * 	then invoke {@link SoundNotifier#registerNoiseMaker(NoiseMaker)}.
 * 
 * @author Eternity
 *
 */
public class DefaultNoiseMaker implements NoiseMaker, IAudioEntity {
	private static IContext sContext;
	private static String mBase;
	private static final IAudioEntity sNullAudio = new NullAudioEntity();
	public static void setContext(IContext pContext, String pBase) {
		sContext = pContext;
		mBase = pBase;
	}
	
	private final float mRange;
	private final String mSoundPath;
	private IEntity mMakerEntity;
	private Sound mNoise;
	
	/**
	 * 
	 * @param pRange in which you can hear its noise.
	 * @param pMakerEntity who make the noise.
	 * @param pSoundPath
	 */
	public DefaultNoiseMaker(float pRange, IEntity pMakerEntity, String pSoundPath) {
		assert pRange >= 0;
		assert pMakerEntity != null;
		
		mRange = pRange;
		mSoundPath = pSoundPath;
		mMakerEntity = pMakerEntity;
	}
	
	@Override
	public float getVoiceRange() {
		return mRange;
	}

	@Override
	public float getX() {
		return mMakerEntity.getX();
	}

	@Override
	public float getY() {
		return mMakerEntity.getY();
	}

	@Override
	public void onVolumeChange(float pLeftVolume, float pRightVolume) {
		if (isNoisy()) {
			mNoise.setVolume(pLeftVolume, pRightVolume);
		}
	}

	@Override
	public void makeNoise() {
		if (!isNoisy()) {
			try {
				mNoise = SoundFactory.createSoundFromAsset(sContext.getSoundManager(), (Context) sContext, mBase + mSoundPath);
			} catch (IOException e) {
				e.printStackTrace();
				Log.e("BaseNoiseMaker", "SoundFactory create sound \"" + mBase + mSoundPath + "\" failed.");
			}
		}
	}

	@Override
	public void behaveQuiet() {
		release();
	}

	@Override
	public boolean isNoisy() {
		return mNoise!=null && !mNoise.isReleased();
	}
	
	private IAudioEntity getAudio() {
		if (!isNoisy()) {
			return sNullAudio;
		}
		return mNoise;
	}

	@Override
	public void play() {
		getAudio().play();
	}

	@Override
	public void pause() {
		getAudio().pause();
	}

	@Override
	public void resume() {
		getAudio().resume();
	}

	@Override
	public void stop() {
		getAudio().stop();
	}

	@Override
	public float getVolume() {
		return getAudio().getVolume();
	}

	@Override
	public void setVolume(float pVolume) {
		getAudio().setVolume(pVolume);
	}

	@Override
	public float getLeftVolume() {
		return getAudio().getLeftVolume();
	}

	@Override
	public float getRightVolume() {
		return getAudio().getRightVolume();
	}

	@Override
	public void setVolume(float pLeftVolume, float pRightVolume) {
		getAudio().setVolume(pLeftVolume, pRightVolume);
	}

	@Override
	public void onMasterVolumeChanged(float pMasterVolume) {
		getAudio().onMasterVolumeChanged(pMasterVolume);
	}

	@Override
	public void setLooping(boolean pLooping) {
		getAudio().setLooping(pLooping);
	}

	@Override
	public void release() {
		getAudio().release();
	}

}
